<template>
  <div class="article-ranking-container">
    <el-card>
      <div class="checkCard">
        <span class="checkTitle">动态展示</span>
        <el-checkbox-group v-model="selectLabel">
          <el-checkbox
            v-for="(item, index) in dynamicData"
            :key="index"
            :label="item.label"
          >{{item.label}}</el-checkbox>
        </el-checkbox-group>
      </div>
    </el-card>
    <el-card>
      <el-table
        ref="articleList"
        :data="articleList"
        border
        style="width: 100%"
      >
        <el-table-column
          v-for="(item, index) in tableColumns"
          :key="index"
          :prop="item.prop"
          :label="item.label"
          :formatter="item.formatter"
        >
          <template v-if="item.prop == 'action'" #default="{ row }">
            <el-button size="mini" type="primary" @click="showArticle(row._id)">{{
              $t("msg.article.show")
            }}</el-button>
            <el-button size="mini" type="danger">{{
              $t("msg.article.remove")
            }}</el-button>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="title" :label="$t('msg.article.title')">
        </el-table-column>
        <el-table-column prop="author" :label="$t('msg.article.author')">
        </el-table-column>
        <el-table-column
          prop="publicDate"
          :label="$t('msg.article.publicDate')"
        >
          <template slot-scope="scope">
            <span>{{ (scope.row.publicDate - newDate) | relativeTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="desc" :label="$t('msg.article.desc')">
        </el-table-column>
        <el-table-column :label="$t('msg.article.action')">
          <template slot-scope="scope">
            <el-button size="mini" type="primary">{{
              $t("msg.article.show")
            }}</el-button>
            <el-button size="mini" type="danger">{{
              $t("msg.article.remove")
            }}</el-button>
          </template>
        </el-table-column> -->
      </el-table>

      <el-pagination
        style="text-align: center; margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticleList } from "../../api/article";
import i18n from "../../i18n/index";
import dayjs from "../../utils/dayjs";
export default {
  data() {
    return {
      articleList: [],
      page: 1,
      size: 10,
      total: 0,
      newDate: "",
      selectLabel: [],
      tableColumns: [],
      dynamicData: [
        {
          label: i18n.t("msg.article.ranking"),
          prop: "ranking",
        },
        {
          label: i18n.t("msg.article.title"),
          prop: "title",
        },
        {
          label: i18n.t("msg.article.author"),
          prop: "author",
        },
        {
          label: i18n.t("msg.article.publicDate"),
          prop: "publicDate",
          formatter: function (value) {
            value = value.publicDate;
            if (!isNaN(value)) {
              value = parseInt(value);
            }
            return dayjs().to(dayjs(value));
          },
        },
        {
          label: i18n.t("msg.article.desc"),
          prop: "desc",
        },
        {
          label: i18n.t("msg.article.action"),
          prop: "action",
        },
      ],
    };
  },
  watch: {
    selectLabel: {
      handler: function (val) {
        this.tableColumns = [];
        const selectData = this.dynamicData.filter((item, index) => {
          return val.includes(item.label);
        });
        this.tableColumns.push(...selectData);
      },
    },
  },
  mounted() {
    this.getArticleList();
    this.initSelectDynamicLabel();
    // this.newDate = Date.parse(new Date()) / 100000000;
  },
  methods: {
    async getArticleList() {
      let res = await getArticleList(this.page, this.size);
      this.articleList = res.list;
      this.total = res.total;
      console.log(res.total);
    },
    handleSizeChange(value) {
      this.size = value;
      this.getArticleList();
    },
    handleCurrentChange(value) {
      this.page = value;
      this.getArticleList();
    },
    initSelectDynamicLabel() {
      this.selectLabel = this.dynamicData.map(item => item.label);
    },
    // 查看
    showArticle(id){
      this.$router.push(`/article/${id}`)
    }
  },
};
</script>

<style scoped lang='scss'>
.checkCard {
  display: flex;
  align-items: center;
}
.checkTitle {
  font-weight: bold;
  font-size: 15px;
  margin-right: 20px;
}
</style>
